<template>
	<div class="request-page">
		<div>
	        <span>环境：</span>
	        <select v-model="mode">
	            <option value="local">本地</option>
	            <option value="test">测试</option>
	            <option value="local4">本地4</option>
	        </select>

            <span>&nbsp;</span>

            <span>方法：</span>
	        <select v-model="method">
	            <option value="post">post</option>
	            <option value="get">get</option>
	        </select>
        </div>
		<div class="mt10">
			<textarea style="width:100%; height:300px; font-size:14px;" v-model="requestData"></textarea>
		</div>
        
        <!-- <div class="content-padded"> -->
            <div class="row">
                <div class="col-70">
                    <input v-model="url" style="width:100%;">
                </div>
                <div class="col-30">
                    <cc-button xsmall @click="queryRequest()">请求</cc-button>
                    <cc-button xsmall @click="queryRequestNew()">提交new</cc-button>
                </div>
            </div>
        <!-- </div> -->

		<!-- <div class="row">
			<div class="col-70">
				<input v-model="url" style="width:100%;">
			</div>
			<div class="col-30">
				<cc-button xsmall full @click="queryRequest2()">提交2</cc-button>
			</div>
		</div> -->

		<div class="row">
			<div class="col-70">
				<input v-model="input.oneCent" style="width:100%;">
			</div>
			<div class="col-30">
				<cc-button xsmall @click="oneCent()">一分钱</cc-button>
				<cc-button xsmall @click="queryOneCent()">查询</cc-button>
			</div>
		</div>

		<div>
			<cc-button @click="buildSign()">xml签名</cc-button>
			<cc-button @click="buildJsonSign()">json签名</cc-button>
			<cc-button @click="buildTestSign()">签名测试</cc-button>
		</div>

		<div>
			<cc-button @click="localNotify()">本地异步通知</cc-button>
		</div>

    </div>
</template>
<script type="text/javascript">

	module.exports = {
		data: function() {
			var space = "http://localhost:7001/space/"
			var inputMap = {
				oneCent : space + "outSystem/oneCent"
			};
			return {
				requestData: '',
                mode: 'local',
                method: 'post',
				// url: upayData.config.getUrl('local'),
				// url : 'http://localhost:7001/space/test/wechat',
				url : 'http://localhost:7001/newpay/yeepay/confirm',
				input : inputMap
			}
		},
		methods: {
			getUrl: function() {
            	return upayData.config.getUrl(this.mode);
        	},

			queryRequest : function() {
				var param2 = {
					data : this.requestData
                }
				var param = Tools.getParam(this.requestData);
                console.log("请求参数", param);
                console.log("请求方法", this.method);
                // Debug.post(this.url, null, "post");
                if(this.method == "post") {
                    Debug.post(this.url, this.requestData, "通用请求");
                } else {
                    Debug.get(this.url, this.requestData, "通用请求");
                }
			},
			queryRequestNew : function() {
				var param2 = {
					data : this.requestData
				};
				var url = 'http://localhost:7001/space/test/wechatNew';
				Debug.post(url, param2, "修复后请求");
			},
			createOutTradeNo : function() {
				var strList = this.requestData.split('\n');
				var noList = new Array();

				for(var index in strList) {
					var num = strList[index].trim();
					noList.push(num);
				}

				var outTradeNo = noList.join(",");
				return outTradeNo;
			},
			oneCent : function() {
				
				var param = {
					outTradeNo : this.createOutTradeNo()
				};
				Debug.post(this.input.oneCent, param, "改一分钱");
			},
			queryOneCent : function() {
				var url = "http://localhost:7001/space/outSystem/queryOneCent";
				var param = {
					outTradeNo : this.createOutTradeNo()
				};
				Debug.post(url, param, "查一分钱", function(data) {
					console.log(JSON.stringify(data));
				});

			},
			// 构建签名
			buildSign : function() {
				var xml = this.requestData;

				var obj = Tools.xmlToObj(xml);

				console.log(obj);
				
				// 对 obj 签名
				var map = Map.getMapBySort(obj);

				console.log(map.toObject())

				var url = Tools.mapToUrl(map);
				console.log(url);

			}, 
			buildJsonSign : function() {
				var json = this.requestData;

				var obj = JSON.parse(json);
				var key = obj.key;
				obj.key = null;

				// 对 obj 签名
				var map = Map.getMapBySort(obj);

				var url = Tools.mapToUrl(map);
				console.log(url);

				var sign = md5(url + "&key=" + key);

				console.log(sign);
			},
			buildTestSign : function() {
				var map = new Map();
				
				map.push("out_trade_no" , "2017112044013054");
				map.push("partner_id" , "dashengtest");
				map.push("trade_status" , "success");
				map.push("notify_time" , "2017-11-20 15:07:31");
				map.push("key" , "ZeZyozUYPaOCX5FP");

				var url = Tools.mapToUrl(map);
				console.log(url);
				var sign = md5(url);
				console.log(sign);
			},
			localNotify : function() {
				var param = {
					test : "test param",
					name : "jianbo"
				};
				var url = this.getUrl() + "/test/localAlipayNotify";
				Debug.post(url, param, "本地支付异步通知");
			}
		}
	}
</script>

<style type="text/css">
	.request-page {}
	.request-page input {
		margin-top: 5px;
		font-size: 13px;
		line-height: 22px;
	}
</style>